package com.vx.vxbug.controllers;

import com.vx.vxbug.BugStatus;
import com.vx.vxbug.entities.Comment;
import com.vx.vxbug.services.BugService;
import com.vx.vxbug.services.CommentService;
import com.vx.vxbug.services.ProgramService;
import com.vx.vxbug.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.GregorianCalendar;

/**
 * Created with IntelliJ IDEA.
 * User: vx
 * Date: 12/4/13
 * Time: 10:10 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class CommentsController {

    @Autowired
    private ProgramService pS;
    @Autowired
    private UserService uS;
    @Autowired
    private BugService bS;
    @Autowired
    private CommentService cS;

    @RequestMapping(value = { "/program/{p}/bug/{b}" }, method = RequestMethod.GET)
    public String getComments(@PathVariable("p") String p, @PathVariable("b") String b, Model model) {
        try {
            int idP = Integer.valueOf(p);
            int idB = Integer.valueOf(b);

            model.addAttribute("statuses", BugStatus.values());
            model.addAttribute("users", uS.getAllUsers());
            model.addAttribute("comments", cS.getAllCommentsForBugId(idB));
            model.addAttribute("b", bS.getBugById(idB));
            model.addAttribute("p", pS.getProgramById(idP));
            return "bug";
        } catch (Exception ex) {
            return "redirect:program";
        }
    }

    @RequestMapping(value = { "/program/{p}/bug/{b}/addComment" }, method = RequestMethod.POST)
    public String addComment(@ModelAttribute("comment")
                                 Comment c, @ModelAttribute(value = "userLogin") String userLogin, @PathVariable("p") String p, @PathVariable("b") String b, Model model) {
        try {
            int idP = Integer.valueOf(p);
            int idB = Integer.valueOf(b);
            int userId = 0;
            if (!userLogin.equals("anonymousUser")){
                userId = uS.getUserByLogin(userLogin).getId();
            }

            GregorianCalendar gc = new GregorianCalendar();
            c.setBugId(bS.getBugById(idB).getId());
            c.setDate(gc.getTime());
            c.setCommenterUserId(userId);
            cS.addComment(c);

            model.addAttribute("statuses", BugStatus.values());
            model.addAttribute("users", uS.getAllUsers());
            model.addAttribute("comments", cS.getAllCommentsForBugId(idB));
            model.addAttribute("b", bS.getBugById(idB));
            model.addAttribute("p", pS.getProgramById(idP));
            return "redirect:";
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return "redirect:";
        }
    }
}
